package com.example.huainanlive.factory.s


import com.google.gson.Gson
import com.google.gson.TypeAdapter
import okhttp3.ResponseBody
import org.json.JSONObject
import retrofit2.Converter
import java.io.IOException
import java.lang.reflect.Type

class CustomResponseConverterThrid<T> internal constructor(
  private val gson: Gson,
  private val adapter: TypeAdapter<out Any>,
  private val mType: Type
) : Converter<ResponseBody, T> {
  @Throws(IOException::class) override fun convert(value: ResponseBody): T {
    val response: AppResponseThrid<*> = AppResponseThrid<Any?>()
    return try {
      val body = value.string()
      val json = JSONObject(body)
      val code = json.optInt("code")
      val msg = json.optString("msg")

      if (code == AppResponseThrid.CODE_SUCCESS) {
        gson.fromJson(body, mType)
      } else {
//        response.msg = msg
        response as T
      }
    } catch (e: Exception) {
      throw RuntimeException(e.message)
    } finally {
      value.close()
    }
  }
}